/**
 * 
 */
package com.jushu.storbox.handler;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.jushu.storbox.LoginActivity;
import com.jushu.storbox.R;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.ExceptionConstant;
import com.jushu.storbox.service.ICloudDriveService;

public class ExceptionHandler extends Handler {

	protected Activity activity;

	protected ICloudDriveService cloudDriveService;

	public ExceptionHandler(Activity activity) {
		this.activity = activity;
	}

	public void handleMessage(Message msg) {
		switch (msg.what) {
		/** 901 server failed */
		case ExceptionConstant.access_Server_Failed:
			exceptionMsg(R.string.loginwait_activity_server_not_response);
			break;
		/** 401 auth failed or disabled */
		case ExceptionConstant.Authorized_Code:
			eventForUnAuthorized(msg.arg1, msg.getData().getString("code"));
			break;
		/** 409 exist the same res */
		case ExceptionConstant.conflict_Code:
			exceptionMsg(R.string.exist_same_resource);
			break;
		/** 403 disabled */
		case ExceptionConstant.Forbidden_Code:
			eventForUserLockedOrForbidden(msg.getData().getString("code"));
			break;
		/** 500 inner error */
		case ExceptionConstant.InternalServerError_Code:
			exceptionMsg(R.string.server_inner_error);
			break;
		/** 405 */
		case ExceptionConstant.Method_Code:
			exceptionMsg(R.string.access_is_forbiden);
			break;
		/** 400  error request or param or range*/
		case ExceptionConstant.Request_Code:
			eventForErrorRequest(msg.getData().getString("code"));
			break;
		/** 412 */
		case ExceptionConstant.requests_Code:
			exceptionMsg(R.string.request_preview_conditions_error);
			break;
		/** 404  res not exist */
		case ExceptionConstant.Resource_Code:
			eventForResNotExist(R.string.resource_is_not_exist);
			break;
		/** 503 Service Unavailable */
		case ExceptionConstant.Service_Unavailable:
			exceptionMsg(R.string.serve_is_not_availible);
			break;
		/** 417 */
		case ExceptionConstant.transaction_Code:
			exceptionMsg(R.string.object_submit_failed);
			break;
		/** default */
		default:
			break;
		}
	}
	

	protected void exceptionMsg(int key) {
		Toast.makeText(activity, activity.getResources().getString(key),
				Toast.LENGTH_SHORT).show();
	}

	protected void eventForResNotExist(int key) {
		exceptionMsg(key);
	}

	protected void eventForUserLockedOrForbidden(String errorMessage) {
		if (ExceptionConstant.UserLocked.equalsIgnoreCase(errorMessage)) {
			if (null != cloudDriveService) {
//				cloudDriveService.clearCloudDriveCache(null);
				exceptionMsg(R.string.login_locked_window_message);
			} else if (ExceptionConstant.forbidden.equalsIgnoreCase(errorMessage)) {
				exceptionMsg(R.string.method_is_forbiden);
			}
		}
	}

	protected void eventForErrorRequest(String errorMessage) {
		if (ExceptionConstant.BadRequest.equalsIgnoreCase(errorMessage)) {
			exceptionMsg(R.string.request_error);
		} else if (ExceptionConstant.invalid_paramter
				.equalsIgnoreCase(errorMessage)) {
			exceptionMsg(R.string.param_error);
		} else if (ExceptionConstant.invalid_range.equalsIgnoreCase(errorMessage)) {
			exceptionMsg(R.string.part_is_not_availible);
		}
	}

	protected void eventForUnAuthorized(int messageFrom, String errorMessage) {
		switch (messageFrom) {
		case ClientConfig.SENDFROMLOGIN:
			if (ExceptionConstant.clientunauthorized
					.equalsIgnoreCase(errorMessage)) {
				if (null != cloudDriveService) {
					cloudDriveService.clearCloudDriveCache(null);
					exceptionMsg(R.string.device_disable);
					cloudDriveService.creatMobileForbiddenDialog();
				}
			} else if (ExceptionConstant.unauthorized
					.equalsIgnoreCase(errorMessage)) {
				exceptionMsg(R.string.passwordiswrong);
			}
			break;
		default:
			exceptionMsg(R.string.device_disable);
			break;
		}
		Intent intent = new Intent();
		intent.setClass(activity, LoginActivity.class);
		activity.startActivity(intent);
		activity.finish();
	}

	public void setCloudDriveService(ICloudDriveService cloudDriveService) {
		this.cloudDriveService = cloudDriveService;
	}

}
